#!/bin/bash

#./dev-setup <amd64_gb28181> <2.00.02> [update]

SELFNANE=$0
VERSION=
OLD_VERSION=
VERSION=$2
DEVICE=$1
UPGRAD=$3
PACK_TIME=$(date "+%Y%m%d%H%M%S")
ARCH="ERROR"

function usage()
{
    echo "A9300 install deb package script"
    echo ""
    echo "$SELFNANE <amd64_A9300|armhf_A9300|amd64_A9800|amd64_gb28181> [2.00.02] [update]"
    echo ""
}

if [[ $DEVICE != "amd64_A9300" ]] && [[ $DEVICE != "armhf_A9300" ]] && [[ $DEVICE != "amd64_A9800" ]] && [[ $DEVICE != "amd64_gb28181" ]] && [[ $DEVICE != "amd64_safety" ]]; then
    usage
    exit 255
fi

if [[ $DEVICE == "amd64_A9300" ]]; then
    ARCH="amd64"
elif [[ $DEVICE == "armhf_A9300" ]];then
    ARCH="armhf"
elif [[ $DEVICE == "amd64_A9800" ]];then
    ARCH="amd64"
elif [[ $DEVICE == "amd64_gb28181" ]];then
    ARCH="amd64"
elif [[ $DEVICE == "amd64_safety" ]];then
    ARCH="amd64"
fi

if [[ $UPGRAD != "update" ]];then

    rm -rf ./work/opt/hrs-server

    if [[ $DEVICE == "amd64_A9300" ]]; then
        git clone -b develop http://192.168.1.27:3000/julong_dev/hw_A9300.git ./work/opt/hrs-server
        ARCH="amd64"
        _RET=$?
    elif [[ $DEVICE == "armhf_A9300" ]];then
        git clone -b develop http://192.168.1.27:3000/julong_dev/hw_A9300_arm.git ./work/opt/hrs-server
        ARCH="armhf"
        _RET=$?
    elif [[ $DEVICE == "amd64_A9800" ]];then
        git clone -b 9800_dev http://192.168.1.27:3000/julong_dev/hw_A9300.git ./work/opt/hrs-server
        ARCH="amd64"
        _RET=$?
    elif [[ $DEVICE == "amd64_gb28181" ]];then
        git clone -b gb28181 http://192.168.1.27:3000/julong_dev/hw_A9300.git ./work/opt/hrs-server
        ARCH="amd64"
        _RET=$?
    elif [[ $DEVICE == "amd64_safety" ]];then
        git clone http://192.168.1.27:3000/hanzheng/A9300_Safety_X86.git ./work/opt/hrs-server
        ARCH="amd64"
        _RET=$?
    fi
    _RET=$?
    if [ $_RET -ne 0 ]; then
        exit 255
    fi
    buildPath=$(pwd)
    cd ./work/opt/hrs-server
    if [ $? -eq 0 ]; then
        echo "current WORKDIR: $(pwd)"
    else
        echo "can't find hrs-server"
    fi
    GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
    GIT_BRANCH_VERSION=$(git rev-parse HEAD)
    GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
    GIT_BRANCH_VERSION=$(git rev-parse HEAD)
    DEV_INFO="Package: hrs-server
Pack-Time: $PACK_TIME
Git-Branch: $GIT_BRANCH
Git-Branch-Version：$GIT_BRANCH_VERSION
Version: $VERSION
Architecture: $ARCH"
    echo "$DEV_INFO" > ./dev_info
    cd $buildPath
    if [ $? -eq 0 ]; then
        echo "current CMD:$(pwd)"
    else
        echo "can't find ${buildPath}"
    fi
    rm -rf ./work/opt/hrs-server/.git
fi

sed -i "s/'version' =>.*/'version' => '$VERSION'/g" ./work/opt/hrs-server/web/api/config.php

INSTALLED_SIZE=`du -s work/opt/hrs-server | awk '{print $1}'`

echo "VERSION = $VERSION"
echo "INSTALLED_SIZE = $INSTALLED_SIZE"

CONTROL="Package: hrs-server
Version: $VERSION
Architecture: $ARCH
Maintainer: Shenzheng Julong Co,.Ltd
Installed-Size: $INSTALLED_SIZE
Depends: mysql-server (>=5.5.8)
Section: admin
Priority: optional
Homepage: http://www.xuean360.com
Description:A hls and rtmp streaming server.
 The hrs Software Foundation's goal is to build a streaming server, can live
 stream of http live stream of mpeg-ts and rtsmp protocal push. The streaming
 result has long been the number one web server on the Internet.
 .
 It features support for HTTPS, can not depends."

echo "$CONTROL" > ./work/DEBIAN/control

if [[ $3 = "update" ]];then
    DEB_NAME="dev-hrs-server-v$VERSION-$DEVICE-$PACK_TIME-update.deb"
else
    DEB_NAME="dev-hrs-server-v$VERSION-$DEVICE-$PACK_TIME.deb"
fi
dpkg-deb -b work/ "$DEB_NAME"
if [ $? -eq 0 ]; then
    echo "create deb ok"
else
    echo "create deb err"
fi
